<?php
class classURL{
	private $url_menu;
	private $url_product;
	private $url_file;

	function __construct(){
		$this->url_menu	 	= $_REQUEST[_URL_VARIABLE_MENU_];
		$this->url_product 	= $_REQUEST[_URL_VARIABLE_PRODUCT_];
		$this->url_file	 	= $_REQUEST[_URL_VARIABLE_FILE_];
		
		//print '   request>'.$this->url_menu.$this->url_product.$this->url_file;
	}
	
	public function SetURL($url_menu, $url_product, $url_file){
		if(_URL_USE_REWRITE_ENGINE_=='YES')
            return self::setRewritedURL($url_menu, $url_product, $url_file);
        else
            return self::setRegularURL($url_menu, $url_product, $url_file);
	}

    private function setRegularURL($url_menu, $url_product, $url_file){
        if($url_menu=='') $new_url_menu	=_URL_VARIABLE_MENU_.'='._DEFAULT_URL_MENU_;
		else{
			if($url_menu!='keep_value')
				$new_url_menu		=_URL_VARIABLE_MENU_.'='.$url_menu;
			else
				$new_url_menu		=_URL_VARIABLE_MENU_.'='.$this->url_menu;
		}

		if($url_product=='') $new_url_product = _URL_VARIABLE_PRODUCT_.'='._DEFAULT_URL_PRODUCT_;
		else{
			if($url_product!='keep_value')
				$new_url_product 	=_URL_VARIABLE_PRODUCT_.'='.$url_product;
			else
				$new_url_product	=_URL_VARIABLE_PRODUCT_.'='.$this->url_product;
		}

		if($url_file=='') $new_url_file =_URL_VARIABLE_FILE_.'='._DEFAULT_URL_FILE_;
		else{
			if($url_file!='keep_value')
				$new_url_file 		=_URL_VARIABLE_FILE_.'='.$url_file;
			else
				$new_url_file		=_URL_VARIABLE_FILE_.'='.$this->url_file;
		}

		return '?'.$new_url_menu.'&amp;'.$new_url_product.'&amp;'.$new_url_file;
    }

    private function setRewritedURL($url_menu, $url_product, $url_file){   
        
        //rewrited URL for news and articles from topmenu
        if(trim($url_menu)!=''){
            $iArticleType   = self::GetValueFromURL($url_menu, 'a_type');
            $iArticleID     = self::GetValueFromURL($url_menu, 'a_text');
            $_as = new classArticleService;
            if($iArticleType==1){
                $URL_TEXT1 = self::makeURL_Text($_as->getNewsName($iArticleID));
                return _SYSTEM_SUBDIRS_._URL_RE_NEWS_CAPTION_.'/'.$URL_TEXT1.'-N'.$iArticleID.'.html';
            }
            if($iArticleType==2){
                $URL_TEXT1 = self::makeURL_Text($_as->getTopCategoryNameOfArticle($iArticleID));
                $URL_TEXT2 = self::makeURL_Text($_as->getArticleName($iArticleID));
                return _SYSTEM_SUBDIRS_.$URL_TEXT1.'/'.$URL_TEXT2.'-UA'.$iArticleID.'.html';
            }            
        }

        //showing products in categoryies, showing products
        if(trim($url_product)!=''){
            //keeping value - it will read value from session
            if($url_product=='keep_value') $url_product = $this->getSerializedURL();
            if($url_product=='') return '';
            //processing variables
            $sOrdering  = self::GetValueFromURL($url_product, 'ordering');
            $iCategory  = self::GetValueFromURL($url_product, 'category');
            $iProduct   = self::GetValueFromURL($url_product, 'product');
            $iPage      = self::GetValueFromURL($url_product, 'page');

            //catergories and product details
            if(is_numeric($iCategory) and $iCategory>0){
                //only show products list in category
                if(trim($iProduct)=='' or !is_numeric($iProduct) or $iProduct<1){
                    $_ps = new classProductService();
                    $asPInfo = $_ps->getTopSubCategoryInfo($iCategory);
                    $URL_TEXT1 = self::makeURL_Text($asPInfo['TC_Caption']);
                    $URL_TEXT2 = self::makeURL_Text($asPInfo['SC_Caption']);
                    return _SYSTEM_SUBDIRS_.$URL_TEXT1.'/'.$URL_TEXT2.'-'.$sOrdering.'-S'.$iCategory.'-A'.$iPage.'/';
                }
                //show detail of product
                if(is_numeric($iProduct) and $iProduct>0){
                    $_ps = new classProductService();
                    $asPInfo = $_ps->getProductAndCategoryInfo($iProduct);
                    $URL_TEXT1 = self::makeURL_Text($asPInfo['TC_Caption']);
                    $URL_TEXT2 = self::makeURL_Text($asPInfo['SC_Caption']);
                    $URL_TEXT3 = self::makeURL_Text($asPInfo['Product_Name']);
                    return _SYSTEM_SUBDIRS_.$URL_TEXT1.'/'.$URL_TEXT2.'-'.$sOrdering.'-S'.$iCategory.'-A'.$iPage.'/'.$URL_TEXT3.'-P'.$iProduct.'.html';
                }
            }

            //new products, closeout sale, actions and product details
            if(is_numeric($iCategory) and $iCategory<1){
                //only show products list in category
                if(trim($iProduct)=='' or !is_numeric($iProduct) or $iProduct<1){

                    switch($iCategory){
                        case -1:{$URL_CAPTION=_URL_RE_ACTIONS_CAPTION_; $URL_VAR='-T1'; break;} //products list in action
                        case -2:{$URL_CAPTION=_URL_RE_NEW_PRODUCTS_CAPTION_; $URL_VAR='-T2'; break;} //list of new products
                        case -3:{$URL_CAPTION=_URL_RE_SALE_CAPTION_; $URL_VAR='-T3'; break;} //list of new products
                    }

                    return _SYSTEM_SUBDIRS_.$URL_CAPTION.$URL_VAR.'-'.$sOrdering.'-A'.$iPage.'/';
                }
                //show detail of product
                if(is_numeric($iProduct) and $iProduct>0){
                    $_ps = new classProductService();
                    $asPInfo = $_ps->getProductAndCategoryInfo($iProduct);
                    $URL_PRODUCT_NAME = self::makeURL_Text($asPInfo['Product_Name']);

                    switch($iCategory){
                        case -1:{$URL_CAPTION=_URL_RE_ACTIONS_CAPTION_; $URL_VAR='-T1'; break;} //products list in action
                        case -2:{$URL_CAPTION=_URL_RE_NEW_PRODUCTS_CAPTION_; $URL_VAR='-T2'; break;} //list of new products
                        case -3:{$URL_CAPTION=_URL_RE_SALE_CAPTION_; $URL_VAR='-T3'; break;} //list of new products
                    }

                    return _SYSTEM_SUBDIRS_.$URL_CAPTION.$URL_VAR.'-'.$sOrdering.'-A'.$iPage.'/'.$URL_PRODUCT_NAME.'-P'.$iProduct.'.html';
                }
            }

        }        
    }

    private function makeURL_Text($sText){
        // Převod českých znaků - vložil překladatel.
        // Původní kód by jinak české mohl odstranit.
        if( function_exists( 'iconv' )){
            // Verze pro knihovnu iconv.
            $sText = iconv( 'utf-8', 'ascii//TRANSLIT//IGNORE', $sText);
        }else{
            // Verze bez iconv.
            $sDiacriticsChars   = 'ľĺěščřžýáíéóôúůďťňĽĹĚŠČŘŽÝÁÍÉÓÚŮĎŤŇ';
            $sReplaceChars      = 'llescrzzaieoouudtnLLESCRZZAIEOUUDTN';
            $sText = strtr( $sText, $sDiacriticsChars, $sReplaceChars );
        }

        // Odstranime vsetky znaky, okrem a-z, 0-9, mínus, podtrzitko alebo mezera.
        $NEakceptovatelneZnaky_regvyr = '#[^-a-zA-Z0-9_ ]#';
        $sText = preg_replace( $NEakceptovatelneZnaky_regvyr, '', $sText);

        // Odstraníme bílé znaky na začátku a na konci.
        $sText = trim($sText);

        // Všechny mínusy, podtržítka a mezery na mínusy.
        $sText = preg_replace('#[-_ ]+#', '-', $sText);

        // Vrátíme upravený řetězec.
        return $sText;
    }
	
	public function GetMenu(){
		return $this->url_menu;
	}
	
	public function GetProduct(){
		return $this->url_product;
	}
	
	public function GetFile(){
		return $this->url_file;
	}

    public function GetValueFromURL($sPart_Url, $sKey){
		$part_url_array = explode(_URL_VAL_DELIMITER_,$sPart_Url);

		switch($sKey){
			case "ordering":	return $part_url_array[0]; break;
			case "category":	return $part_url_array[1]; break;
			case "product":		return $part_url_array[2]; break;
            case "page":		return $part_url_array[3]; break;
            case "a_type":		return $part_url_array[0]; break;
            case "a_text":		return $part_url_array[1]; break;
		}
	}

    public function URL_serialize($sURLValue){
        $_session = new classSessions();
        $_session->open('StoredURL');
        $_session->serializeIt($sURLValue);
    }

    private function getSerializedURL(){
        $_session = new classSessions();
        return $_session->open('StoredURL');
    }

}
?>